package com.edgclub.edg.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.View.OnClickListener;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.LikeMembersAdapter;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.TopicApi;
import com.edgclub.edg.api.param.PostsParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.UserListData;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;


/**
 */
public class GzLikeMembersActivity extends GzBaseTooolBarActivity implements
        OnClickListener ,VolleyListener{

    private XRecyclerView recyclerView;
    private int currPage = 1;
    private int pageSize = 20;
    private LikeMembersAdapter likeMembersAdapter;
    private UserPrefUtils userPrefUtils;
    private String topicId;
    private List<User>userList;

    private TopicApi topicApi;
    @Override
    public int getContentView() {
        return R.layout.activity_group_people  ;
    }
    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("点赞列表");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }
    @Override
    public void initView() {
        recyclerView= (XRecyclerView) findViewById(R.id.recyclerview);
    }
    @Override
    public void initData() {
        showDialog1();
        topicApi = new TopicApi(this);
        userPrefUtils = new UserPrefUtils(this);
        topicId=getIntent().getStringExtra("topicId");
        likeMembersAdapter= new LikeMembersAdapter(this);
        userList=new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        likeMembersAdapter.setDatas(userList);
        recyclerView.setAdapter(likeMembersAdapter);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currPage = 1;
                getlikemembers();
            }

            @Override
            public void onLoadMore() {
                getlikemembers();
            }
        });
        getlikemembers();
    }

    private void getlikemembers() {
        PostsParam postsParam = new PostsParam();
        postsParam.setPostsId(topicId);
        postsParam.setUserId(userPrefUtils.getUser().getId() + "");
        postsParam.setPageSize(pageSize + "");
        postsParam.setPage(currPage + "");
        new TopicApi(this).getlikeMembers(postsParam, this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            default:
                break;
            case R.id.main_back:
                finish();
                break;
        }
    }


    @Override
    public void onStart(String tag) {
//        showDialog("正在加载...",false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        if (JSONHelper.isSuccess(response)) {
            UserListData data = JsonUtil.getMode(response,
                    UserListData.class);
            if(null!=data&&null!=data.getData() && data.getData().size()>0){
                if(currPage==1){
                    userList.clear();
                }
                currPage++;

                userList.addAll(data.getData());
                likeMembersAdapter.notifyDataSetChanged();
            }else{
                recyclerView.noMoreLoading();
            }
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        dismissDialog();
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        setNoNetWork(new ReLoadClickListener() {
            @Override
            public void reload() {
                getlikemembers();
            }
        });
        ResultCode.toastVolleyError(this, volleyError);
    }
}
